<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 01:21:39
 * @LastEditTime: 2023-10-31 15:47:06
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Traits;

use App\Models\ComprehensiveCarry;
use App\Models\ComprehensiveCarryDetails;
use App\Repositories\ComprehensiveCarryRepository;
use Illuminate\Support\Facades\App;

trait CarryTrait
    {
    use CarryCarDetailTrait;
    public function CarryTraitDeleting()
        {
        $this->removeCarryItem();
        $this->removeCarCarryDetail($this::class, $this->getCarrySourceFieldKey(), $this->{$this->getCarrySourceFieldKey()});
        }
    public function CarryTraitCreated($model)
        {
        $quantity_field           = $model->getCarryItemQuantityField();
        $auxiliary_quantity_field = $model->getCarryItemAuxiliaryQuantityField();
        return $this->createCarCarryDetail(
            $model->material_id,
            $model->dyelot,
            $model->type_id,
            $model->batchno,
            $model->color_name,
            $model->{$auxiliary_quantity_field},
            $model->{$quantity_field},
                $model::class,
            $this->getCarrySourceFieldKey(),
            $model->{$this->getCarrySourceFieldKey()}
        );
        }

    public function CarryTraitUpdated($model)
        {
        return $this->CarryTraitCreated($model);
        }
    /**
     * 获取车车模型
     * @return ComprehensiveCarryRepository
     * @author sunkaiyuan
     */
    public function getCarryRepository()
        {
        return App::make(ComprehensiveCarryRepository::class);
        }
    /**
     * 车与本单关联的关键字
     * @var string
     * @author sunkaiyuan
     */
    private $_carryKey = 'carry_id';
    private $_carry_source_field = 'id';
    private $_quantityField = 'quantity';
    private $_auxiliary_quantity_field = 'auxiliary_quantity';
    private $_carry_count = 1;
    public function getCarryItemQuantityField()
        {
        $key = $this->_quantityField;
        if (property_exists(__CLASS__, "quantityField"))
            $key = $this->quantityField;
        return $key;
        }
    public function getCarryItemAuxiliaryQuantityField()
        {
        $key = $this->_auxiliary_quantity_field;
        if (property_exists(__CLASS__, "auxiliary_quantity_field"))
            $key = $this->auxiliary_quantity_field;
        return $key;
        }

    public function carryItem()
        {
        $key = $this->getCarrySourceFieldKey();
        return $this->hasOne(ComprehensiveCarryDetails::class, "source_value", $key)->where(function ($query) use ($key)
            {
            $query->where("source_key", $key)
                ->where("source_model", $this::class);
            });
        }
    /**
     * Summary of carry
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author sunkaiyuan
     */
    public function carry()
        {
        return $this->belongsTo(ComprehensiveCarry::class, $this->getCarryKey());
        }
    /**
     * 获取车关联关键字段
     * @return mixed
     * @author sunkaiyuan
     */
    public function getCarrySourceFieldKey()
        {
        $key = $this->_carry_source_field;
        if (property_exists(__CLASS__, "carrySourceField"))
            $key = $this->CarrySourceField;
        return $key;
        }
    /**
     * 获取车关联关键字
     * @return mixed
     * @author sunkaiyuan
     */
    public function getCarryKey()
        {

        $key = $this->_carryKey;
        if (property_exists(__CLASS__, "carryKey"))
            $key = $this->carrykey;
        return $key;
        }
    /**
     * 新增派车，必须有明细
     * @param mixed $carry_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function addCarry($code, $carry_id = null)
        {
        $carryRepos = $this->getCarryRepository();

        $carry_id = $this->_getCarryId($carry_id);
        if ($carry_id === null) {
            return;
            }
        return $this->details->filter(function ($item) use ($carry_id, $carryRepos, $code)
            {
            return $carryRepos->addItem($code, $carry_id, $item);
            });
        }
    /**
     * 删除派车单，必须有明细
     * @param mixed $carry_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function removeCarry($carry_id = null)
        {
        $carryRepos = $this->getCarryRepository();
        $carry_id   = $this->_getCarId($carry_id);

        return $this->details->filter(function ($item) use ($carry_id, $carryRepos)
            {
            return $carryRepos->removeItem($carry_id, $item);
            });
        }
    /**
     * 增加派车明细，自己就是明细
     * @param mixed $carry_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function addCarryItem($carry_id = null, $code = null)
        {
        $carry_id = $this->_getCarryId($carry_id);
        if ($carry_id === null) {
            return;
            }

        $isnew = $this->getCarryRepository()->addItem($code ?? $this->code, $carry_id, $this);
        if ($isnew) {
            $detail              = $this->getCar_carry_detailAttribute($this::class, $this->getCarrySourceFieldKey(), $this->{$this->getCarrySourceFieldKey()});
            $detail->carry_count = 0;
            $detail->save();
            }
        }
    /**
     * 获取车Id
     * @param mixed $carry_id
     * @return mixed
     * @author sunkaiyuan
     */
    private function _getCarryId($carry_id = null)
        {
        return $carry_id ?? $this->{$this->getCarryKey()};
        }
    /**
     * 从派车明细中删除
     * @param mixed $carry_id
     * @return mixed
     * @author sunkaiyuan
     */
    public function removeCarryItem($carry_id = null)
        {
        $detail = $this->getCar_carry_detailAttribute($this::class, $this->getCarrySourceFieldKey(), $this->{$this->getCarrySourceFieldKey()});
        if ($detail) {
            $detail->carry_count = 1;
            $detail->save();
            }

        $carry_id = $this->_getCarryId($carry_id);
        if ($carry_id === null) {
            return;
            }

        return $this->getCarryRepository()->removeItem($carry_id, $this);
        }

    public function scopeAddCarry($source_addr, $target_addr, $carry_id = null)
        {
        return $this->get()->filter(function ($v) use ($carry_id, $source_addr, $target_addr)
            {
            return $v->addCarry($source_addr, $target_addr, $carry_id);
            });
        }

    public function scopeAddCarryItem($carry_id = null, $code = null)
        {
        return $this->get()->filter(function ($v) use ($carry_id, $code)
            {
            return $v->addCarryItem($carry_id, $code);
            });
        }
    public function scopeRemoveCarryItem($carry_id = null)
        {
        return $this->get()->filter(function ($v) use ($carry_id)
            {
            return $v->removeCarryItem($carry_id);
            });
        }
    public function scoperemoveCarry($carry_id = null)
        {
        return $this->get()->filter(function ($v) use ($carry_id)
            {
            return $v->removeCarry($carry_id);
            });
        }
    }